package com.da.web.sys_role.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.da.common.Result;
import com.da.web.sys_role.entity.*;
import com.da.web.sys_role.service.SysRoleService;
import com.da.web.sys_role_menu.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author da
 * @time 2023/12/1 下午 4:27
 */
@RestController
@RequestMapping("/api/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    //新增
    @PostMapping
    public Result addRole(@RequestBody SysRole role) {
        role.setCreateTime(new Date());
        boolean save = sysRoleService.save(role);
        if (save) {
            return Result.success("新增角色成功");
        }
        return Result.error("新增角色失败");
    }

    //编辑
    @PutMapping
    public Result editRole(@RequestBody SysRole role) {
        role.setUpdateTime(new Date());
        boolean save = sysRoleService.updateById(role);
        if (save) {
            return Result.success("编辑角色成功");
        }
        return Result.error("编辑角色失败");
    }

    //删除
    @DeleteMapping("/{roleId}")
    public Result deleteRole(@PathVariable("roleId") Long roleId) {
        boolean remove = sysRoleService.removeById(roleId);
        if (remove) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败!");
    }

    //角色列表
    @GetMapping("/list")
    public Result getList(RoleParm parm) {
        IPage<SysRole> list = sysRoleService.list(parm);
        return Result.success("查询成功", list);
    }

    @GetMapping("/getAssignShow")
    public Result getAssignShow(AssignParm parm) {
        AssignVo show = sysRoleService.getAssignShow(parm);
        return Result.success("查询成功", show);
    }

    @PostMapping("/saveRoleMenu")
    public Result saveRoleMenu(@RequestBody SaveRoleParm parm) {
        sysRoleMenuService.saveRoleMenu(parm.getRoleId(), parm.getList());
        return Result.success("分配权限成功");
    }
}
